package com.example.async_dmoe1.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Auther: 杨通通
 * @Date: 2024/9/20 00:09
 * @Description:
 */
@Service
public class HelloService {



    public String hello1(){
        System.out.println(Thread.currentThread().getName());//和controller的线程是同一个
        return "HelloService hello1";


    }

    //不加@Async线程是tomcat的线程池的主线程，加了后会另启线程
    @Async
    public  void asyncTask(){
        try {
            Thread.sleep(5*1000);
            System.out.println(Thread.currentThread().getName());//和controller中的线程不是同一个，controller不能获取返回值
            System.out.println("exec async task");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}